<script setup>
import { ElMessage } from 'element-plus'
import 'element-plus/theme-chalk/el-message.css'
// 导入登录接口loginAPI
// import { loginAPI } from '@/apis/user'
// 表单校验（账号名＋密码）
// 准备表单对象
import { ref } from 'vue'
//导入 router对象
import { useRouter } from 'vue-router'
//导入用户store
import { useUserStore } from '@/stores/user'
//导入购物车列表
import { useCartStore } from '@/stores/cartStore'
// 获取用户store实例
const userStore = useUserStore()
//创建cart实例
const cartStore = useCartStore()
// 创建router实例
const router = useRouter()
const form = ref({
  password: '',
  account: '',
  agree: true,
  role: ''
})
// 准备规则对象
// 规则数据对象
const rules = {
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { min: 6, max: 24, message: '密码长度要求6-14个字符' }
  ],
  account: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
  agree: [
    {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
}
// 获取form表单实例统一校验
const formRef = ref(null)
const doLogin = () => {
  const { account, password, role } = form.value
  // 调用实例方法
  formRef.value.validate(async (valid) => {
    // valid: 所有表单都通过校验  才为true
    console.log(valid)
    // 以valid做为判断条件 如果通过校验才执行登录逻辑
    if (valid) {
      // TODO LOGIN
      // await loginAPI({ account, password })
      await userStore.getUserInfo({ account, password })
      // 列表更新
      await cartStore.updateCart()
      // 1. 提示用户
      ElMessage({ type: 'success', message: '登录成功' })
      // 2.根据角色跳转到不同页面
      if (role === 'admin') {
        // 跳转到管理员系统
        router.replace({ path: '/admin' })
      } else if (role === 'merchant') {
        // 跳转到商家系统
        router.replace({ path: '/merchant' })
      } else {
        // 普通用户跳转到首页
        router.replace({ path: '/' })
      }
    }
  })
}
</script>

<template>
  <div>
    <!-- 头部区域 -->
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">淘宝</RouterLink>
        </h1>
        <!-- 头部进入网站首页区域 -->
        <RouterLink class="entry" to="/">
          首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <!-- 中间区域部分 -->
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">欢迎来到天猫超市购物商城</a>
        </nav>
        <!-- 登录区域 -->
        <div class="account-box">
          <div class="form">
            <el-form ref="formRef" :model="form" :rules="rules" status-icon>
              <!-- 账户验证区域 -->
              <el-form-item prop="account" label="账户">
                <el-input v-model="form.account" placeholder="请输入账户" />
              </el-form-item>
              <!-- 密码验证区域 -->
              <el-form-item prop="password" label="密码">
                <el-input v-model="form.password" placeholder="请输入密码" />
              </el-form-item>
              <!-- 下拉框选择角色 -->
              <el-form-item prop="role" label="角色">
                <el-select v-model="form.role" placeholder="请选择角色">
                  <el-option label="用户" value="user"></el-option>
                  <!-- <el-option label="商家" value="merchant"></el-option> -->
                  <el-option label="管理员" value="admin"></el-option>
                </el-select>
              </el-form-item>

              <!-- 隐私协议勾选区域 -->
              <el-form-item label-width="22px" prop="agree">
                <el-checkbox size="large" v-model="form.agree">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button
                size="large"
                class="subBtn"
                style="background-color: #af3939"
                @click="doLogin"
                >登录</el-button
              >
            </el-form>
          </div>
        </div>
      </div>
    </section>
    <!-- 底部区域 -->
    <footer class="login-footer">
      <div class="container">
        <p>Copyright © 2024 天猫超市购物商城</p>
        <p>仲恺小短腿有限公司 版权所有</p>
        <p>联系我们 &nbsp; | &nbsp; 隐私政策 &nbsp; | &nbsp; 法律声明</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang="scss">
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 100px;
      width: 100%;
      text-indent: -9999px;
      background: url('@/assets/images/logo.png') no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  min-height: 450px;
  // 计算高度，减去头部和底部的高度
  height: calc(100vh - 300px);
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 30%;
    top: 15%;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 0 0 50px;
  background: #fff;
  height: 100px;
  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~ a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        > i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      > .error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>
